/*******************************************************************************
 Jimm - Mobile Messaging - J2ME ICQ clone
 Copyright (C) 2003-05  Jimm Project
 Copyright (C) 2011-12  Jimm for Android Project

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ********************************************************************************
 File: src/jimm/comm/Message.java
 Version: ###VERSION###  Date: ###DATE###
 Author(s): Manuel Linsmayer, Andreas Rossbacher
 *******************************************************************************/

package org.jimm.message;

import org.jimm.app.JimmApplication;
import org.jimm.app.R;
import org.jimm.protocol.Contact;
import org.jimm.protocol.Protocol;

import android.util.Log;
import android.view.View;


public abstract class Message {
    public static final int ICON_SYSREQ = 0;
    public static final int ICON_SYS_OK = 1;
    public static final int ICON_TYPE = 2;
    public static final int ICON_MSG_NONE = 3;
    public static final int ICON_MSG_FROM_SERVER = 4;
    public static final int ICON_MSG_FROM_CLIENT = 5;
    //public static final int ICON_ERROR = 6;

    public static final int NOTIFY_OFF = -1;
    public static final int NOTIFY_NONE = ICON_MSG_NONE;
    public static final int NOTIFY_FROM_SERVER = ICON_MSG_FROM_SERVER;
    public static final int NOTIFY_FROM_CLIENT = ICON_MSG_FROM_CLIENT;


    protected boolean isIncoming;

    protected String contactUin;
    protected Contact contact;
    protected Protocol protocol;
    private View convertView;
    private String senderName;
    // Date of dispatch
    private long newDate;
    private int state;
    public boolean isReaded = false;

    protected Message(long date, Protocol protocol, String contactUin, boolean isIncoming) {
        newDate = date;
        this.protocol = protocol;
        this.contactUin = contactUin;
        this.isIncoming = isIncoming;

        // ������������ ��������� �� ����� ���� �������������.
        if (!isIncoming) isReaded = true;
    }

    protected Message(long date, Protocol protocol, Contact contact, boolean isIncoming) {
        newDate = date;
        this.protocol = protocol;
        this.contact = contact;
        this.isIncoming = isIncoming;

        // ������������ ��������� �� ����� ���� �������������.
        if (!isIncoming) isReaded = true;
    }

    public void setConvertView(View convertView) {
        this.convertView = convertView;

    }

    public View getConvertView() {
        return convertView;
    }

    public final void setSendingState(int state) {
//        if (null != par) {
//            par.replaceFirstIcon(msgIcons[state]);
//            Contact rcvr = getRcvr();
//            if (rcvr.hasChat()) {
//                protocol.getChat(rcvr).invalidate();
//            }
//        }
        Log.i("Message", "setSendingState: contactUin = " + getContactUin() + ", state = " + state);


        this.state = state;
        JimmApplication.getInstance().getService().getCurrentActivity().invalidate();
    }

    public final void setName(String name) {
        senderName = name;
    }

    private String getContactUin() {
        return (null == contact) ? contactUin : contact.getUserId();
    }

    // Returns the senders UIN
    public final String getSndrUin() {
        return isIncoming ? getContactUin() : protocol.getUserId();
    }

    // Returns the receivers UIN
    public final String getRcvrUin() {
        return isIncoming ? protocol.getUserId() : getContactUin();
    }

    public final boolean isIncoming() {
        return isIncoming;
    }

    public final boolean isReaded() {
        return isReaded;
    }

    // Returns the receiver
    public final Contact getRcvr() {
        return (null == contact) ? getProtocol().getItemByUIN(contactUin) : contact;
    }

    public final Protocol getProtocol() {
        return protocol;
    }

    public boolean isOffline() {
        return false;
    }

    public final long getNewDate() {
        return newDate;
    }

    public String getName() {
        if (null == senderName) {
            Contact c = getRcvr();
            if (isIncoming) {
                senderName = (null == c) ? getContactUin() : c.getName();
            } else {
                senderName = (null == c) ? protocol.getNick() : c.getMyName();
            }
        }
        return senderName;
    }

    public abstract String getText();

    public String getProcessedText() {
        return getText();
    }

    public boolean isWakeUp() {
        return false;
    }

    public int getTextColor() {
        if (isIncoming())
            return R.color.chat_contact_name_b;

        return R.color.chat_contact_name_a;
    }

    public int getIcon() {
        if (isIncoming == false) {
            switch (state) {
                case 0:
                    return R.drawable.ic_message_outgoing;

                case NOTIFY_FROM_CLIENT:
                    return R.drawable.ic_message_delivered;

                case NOTIFY_FROM_SERVER:
                    return R.drawable.ic_message_outgoing;
            }
        }
        return R.drawable.ic_message_incoming;
    }
}